package com.ilop.permission.gps;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * @author skygge
 * @date 6/21/22.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class GpsPresenter {

    private Context mContext ;

    private Receiver receiver ;

    private final OnGpsCallBack mInterface ;

    private final String GPS_ACTION = "android.location.PROVIDERS_CHANGED" ;

    public GpsPresenter(Context context , OnGpsCallBack mInterface ){
        this.mContext = context ;
        this.mInterface = mInterface ;
        observeWifiSwitch();
    }

    private void observeWifiSwitch(){
        IntentFilter filter = new IntentFilter();
        filter.addAction(GPS_ACTION);
        receiver = new Receiver() ;
        mContext.registerReceiver(receiver, filter);
    }

    /**
     * 释放资源
     */
    public void onDestroy() {
        if ( receiver != null ){
            mContext.unregisterReceiver( receiver );
        }
        if (mContext!=null){
            mContext = null;
        }
    }

    class Receiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().matches(GPS_ACTION)) {
                if (mInterface != null) mInterface.onGpsRequest();
            }
        }
    }
}
